Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change use of the EnergyExported attribute to EnergyImported in matter switch #1830

Merged
merged 4 commits into from
Jan 4, 2025

Conversation

hcarter-775
Copy link
Contributor

@hcarter-775 hcarter-775 commented Dec 17, 2024

Check all that apply

Type of Change

  • WWST Certification Request
    • If this is your first time contributing code:
      • I have reviewed the README.md file
      • I have reviewed the CODE_OF_CONDUCT.md file
      • I have signed the CLA
    • I plan on entering a WWST Certification Request or have entered a request through the WWST Certification console at developer.smartthings.com
  • Bug fix
  • New feature
  • Refactor

Checklist

  • I have performed a self-review of my code
  • I have commented my code in hard-to-understand areas
  • I have verified my changes by testing with a device or have communicated a plan for testing
  • I am adding new behavior, such as adding a sub-driver, and have added and run new unit tests to cover the new behavior

Description of Change

According to internal review as well as MTR-873, the ElectricalEnergyMeasurement cluster's imported attributes should be used for positive energy values rather than the exported attributes. This change commits that statement to the drivers.

Summary of Completed Tests

All of the extensive unit tests have been converted from exported to imported tests, which properly handles all of the cases we're most interested in.

@hcarter-775 hcarter-775 changed the title Change use of (Periodic/Cumulative)EnergyExported attribute to (Periodic/Cumulative)EnergyImported in matter switch Change use of the EnergyExported attribute to EnergyImported in matter switch Dec 17, 2024
Copy link

github-actions bot commented Dec 17, 2024

Channel deleted.

Copy link

github-actions bot commented Dec 17, 2024

File Coverage
All files 94%
/home/runner/work/SmartThingsEdgeDrivers/SmartThingsEdgeDrivers/drivers/SmartThings/matter-switch/src/eve-energy/init.lua 91%
/home/runner/work/SmartThingsEdgeDrivers/SmartThingsEdgeDrivers/drivers/SmartThings/matter-switch/src/embedded-cluster-utils.lua 38%
/home/runner/work/SmartThingsEdgeDrivers/SmartThingsEdgeDrivers/drivers/SmartThings/matter-switch/src/init.lua 97%
/home/runner/work/SmartThingsEdgeDrivers/SmartThingsEdgeDrivers/drivers/SmartThings/matter-switch/src/aqara-cube/init.lua 96%

Minimum allowed coverage is 90%

Generated by 🐒 cobertura-action against 66cf375

Copy link

github-actions bot commented Dec 17, 2024

Test Results

   64 files    403 suites   0s ⏱️
2 006 tests 2 006 ✅ 0 💤 0 ❌
3 468 runs  3 468 ✅ 0 💤 0 ❌

Results for commit 66cf375.

♻️ This comment has been updated with latest results.

@@ -0,0 +1,68 @@
local cluster_base = require "st.matter.cluster_base"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we be able to remove the CumulativeEnergyExported and PeriodicEnergyExported embedded cluster defs now that we are just using the imported variation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. I figured why not leave them in, but it's just as well to take them out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in 438b6252f08fba1d

local current_time = os.time()
local last_time = device:get_field(LAST_EXPORTED_REPORT_TIMESTAMP) or 0
device:set_field(LAST_EXPORTED_REPORT_TIMESTAMP, current_time, { persist = true })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag will have been persisted in the case on devices that have already joined to the platform and run this code. However, it wouldn't be needed anymore so we should set it to nil and remove it.

However, I don't think there are a lot/any devices like this yet, so in this case we are okay to just remove this, but just something to be aware of for the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To note: I have not removed anything- only renamed the word export to import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nevermind I see what you mean. Thanks for pointing that out!

Copy link
Contributor Author

@hcarter-775 hcarter-775 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update on this: from the superset query counting all matter profiles in use, I'm seeing:
Matter Switch plug-power-energy-powerConsumption 510, so there are 510 of these devices that would have run this code already. Note: this code would only run if the device supports powerConsumption, so no need to count any devices that don't profile to this.

@ctowns Do you think we should add extra handling to transition these devices?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you see what device these are? If it's the eve energy plug, then those devices would use the eve-energy sub driver, so then we technically wouldn't need to worry about clean up for those since they wouldn't have run this code, they would have just used the sub driver.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctowns These wouldn't be for the eve-energy driver, since those devices still fingerprints to this power-energy-powerConsumption. You can see these in the query as well: Matter Switch power-energy-powerConsumption 9516. The 512 would only have come from the 1.3 support

Copy link
Contributor

@ctowns ctowns Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for clarifying! So is it reasonable to assume that the 510 devices just aren't working in terms of power reports right now? If we are reading the wrong attribute, then I would assume the data we are getting is wrong so I'm not sure what "transition" we'd need to do clean up wise since the data would've just been wrong. I assume we can't change it once it's made it to ST energy. So in that case, I guess the only clean up we could do would be setting the export fields to nil. FWIW, ultimately this would save just a few bytes of memory per device.

I am surprised there are this many devices using this driver already, I would suggest we figure out what these devices are and try to procure one so we can test this functionality with a real device! That could give us some more confidence moving forward.

Copy link
Contributor Author

@hcarter-775 hcarter-775 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should be the case. In fact, if these devices are correctly using the 'Imported' attributes, we would not have read anything at all, so nothing would have even been reported to ST Energy in the first place, since the report reads TOTAL_IMPORTED_ENERGY, which is only set in the handlers.

Further, if anyone were incorrectly using the 'exported' attribute like us, there's this logic gate in the report:
if previous_imported_report and previous_imported_report.energy then energy_delta_wh = math.max(latest_total_imported_energy_wh - previous_imported_report.energy, 0.0) end
which will make sure it never reported a negative value.

Therefore, by this logic we shouldn't have reported anything incorrectly so far to ST Energy except sending a lot of nil values from the fact that we're never updating TOTAL_IMPORTED_ENERGY.

And as for saving a few bytes like you mentioned, I'm not sure this is worth it either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, a couple bytes is unimportant especially for these low device counts. However, I think it is still a good idea to look into procuring one of the devices that actually uses the new clusters defs so we can do testing with this on a real device. This would also help with testing against older lua libs on a real device.

@hcarter-775 hcarter-775 mentioned this pull request Dec 27, 2024
12 tasks
local CumulativeEnergyImported = {
ID = 0x0001,
NAME = "CumulativeEnergyImported",
base_type = require "st.matter.generated.zap_clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This st.matter.generated.zap_clusters prefix needs to be removed for this to work properly on hub running older lua libs where these definitions do not exist, same in the file below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

@ctowns ctowns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I would run the unit tests against an older version of the lua libs locally. Also, let's get a physical device as soon as we can

@hcarter-775 hcarter-775 merged commit f4cefe9 into main Jan 4, 2025
11 checks passed
@hcarter-775 hcarter-775 deleted the MTR-873 branch January 4, 2025 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants